<?php

/**
 * Class for viewing a nested CSS selector.
 *
 * @author Ian Clarence ian.clarence@gmail.com
 */ 
class ViewNestedSelector {
    
    /**
     * Array of objects of type ViewSelector. 
     */
    private $selectors = array();

    /**
     * The constructor.
     *
     * @param array $selectors
     */
    public function __construct(array $selectors = array()) {      
        if (count($selectors > 0)) {
            foreach ($selectors as $selectorKey => $selector) {
                if ($selector instanceof ViewSelector) {
                    $this->selectors[$selectorKey] = $selector;
                }
            }
        }
    }

    /**
     * Returns a simple nested CSS selector.
     *
     * @param string $tag
     * @param string $id
     * @param string $className
     * @param string $pseudoClass
     * @return ViewNestedSelector
     */
    public static function nestedSelectorInit(
        $tag = NULL, 
        $id = NULL, 
        $className = NULL, 
        $pseudoClass = NULL
    ) {
        return new self(array(ViewSelector::selectorInit($tag, $id, $className, $pseudoClass)));
    }
    
    /**
     * Viewing the nested selector.
     *
     * @return string
     */
    public function view() {
        if (count($this->selectors) == 0) {
            return '';
        }
        $output = array();
        foreach ($this->selectors as $selector) {
            $output[] = $selector->view();
        }
        return implode(' ', $output);
    }
    
}